package com.healthcare.apm.category.util;

import lombok.Getter;
import lombok.Setter;
import org.apache.logging.log4j.util.Strings;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class IKAnalyzer4Lucene7 extends Analyzer {

    @Getter
    @Setter
    private boolean useSmart = false;

    public IKAnalyzer4Lucene7() {
        this(false);
    }

    public IKAnalyzer4Lucene7(boolean useSmart) {
        super();
        this.useSmart = useSmart;
    }

    @Override
    protected TokenStreamComponents createComponents(String fieldName) {
        IKTokenizer4Lucene7 tk = new IKTokenizer4Lucene7(this.useSmart);
        return new TokenStreamComponents(tk);
    }


    static public List<String> splitText(String etext){
        List<String> res = new ArrayList<>();
        if(Strings.isBlank(etext)){
            return res;
        }
        try (Analyzer ik = new IKAnalyzer4Lucene7();){
            TokenStream ts = ik.tokenStream("content", etext);
            ts.reset();
            CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class);
            while (ts.incrementToken()) {
                res.add(cta.toString());
            }
            ts.end();
            ts.close();
        } catch (IOException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
        return res;
    }

}